<?php  
	namespace Home\Controller;
	use Think\Controller;
    //学员管理
    //@author  程传民
	class UserController extends AllowController{

        //登录进去之后的首页
        public function index(){

            $homenotice = D("homenotice");

            //校内公告
            $list1 = $homenotice->where(array("articletype"=>1))->order("addtime desc")->select();
            //全校公告
            $list2 = $homenotice->where(array("articletype"=>2))->order("addtime desc")->select();
            //社会公告
            $list3 = $homenotice->where(array("articletype"=>3))->order("addtime desc")->select();
            
            $this->assign("list1",$list1);
            $this->assign("list2",$list2);
            $this->assign("list3",$list3);
            $this->display("User/index");  
        }

        //点击查看公告内容
        public function noticedetails(){
            $arr = array("id"=>I("get.id"));
            $homenotice = D("homenotice");
            $list = $homenotice->where($arr)->find();
            $this->assign("list",$list);
            $this->display("User/noticedetails");
        }
        
        //用户列表页
        public function pub(){
            $this->assign("username",$_SESSION['name']);
            $this->assign("pic",$_SESSION['pic']);
            if($_SESSION['role_id']>7){
                $this->display("Public/StuPublic");
            }else{
                $this->display("Public/TeacherPublic");
            }
        }


        //基本信息
        public function basic(){
        	// $name = $_SESSION['name'];
        	$id = $_SESSION['userid'];
        	$user = D("users");
        	$arr = array("id"=>$id);
        	$list = $user->where($arr)->find();
       

        	if($list['can_login']!=0){
        		$status = "允许登录";
        	}else{
        		$status = "用户已停用";
        	}
        	$list['can_login'] = $status;

            $class = D("class");
            $a = array("id"=>$list['class']);
            $cls = $class->where($a)->find();
            $list['class'] = $cls['name'];

        	$team = D("team");
        	$arrs = array("id"=>$list["team"]);
        	$res = $team->where($arrs)->find();
            if($res){
        	   $list["team"] = $res['name'];
            }else{
                $list['team'] = "暂无";
            }

        	$this->assign("list",$list);
        	$this->display("User/basicInfo");
        }

        //详情信息
        public function preInfo(){

        	$id = $_SESSION['userid'];
        	
        	$user = D("users");
        	$arr = array("id"=>$id);
        	$list = $user->where($arr)->find();

        	if($list1['can_login']!=0){
        		$status = "允许登录";
        	}else{
        		$status = "用户已停用";
        	}
        	$list['can_login'] = $status; 

            $class = D("class");
            $a = array("id"=>$list['class']);
            $cls = $class->where($a)->find();
            $list['class'] = $cls['name'];

        	$team = D("team");
            $arrs = array("id"=>$list["team"]);
            $res = $team->where($arrs)->find();
            if($res){
               $list["team"] = $res['name'];
            }else{
                $list['team'] = "暂无";
            }

            $class = D("class");
            $classs = $class->field("id,name")->select();

            foreach($classs as $v){
                if($list['class'] == $v['id']){
                    $list['class'] = $v['name'];
                }
            }

        	$this->assign("list",$list);
        	$this->display("User/detailsInfo");
        }


        //修改个人信息
        public function update(){
        	//users表中的id  userinfo中的pid
        	$where = array("id"=>I("post.id"));
        	$mod = D("users");

        	$data['sex'] = I("post.sex");
        	$data['card'] = I("post.card");
        	$data['address'] = I("post.address");
        	$data['phone'] = I("post.phone");
        	$data['qq'] = I("post.qq");
        	$data['email'] = I("post.email");
            $data['school_address'] = I("post.school_address");
        	$data['school'] = I("post.school");
        	$data['education'] = I("post.education");
        	$data['subject'] = I("post.subject");
        	$data['computer'] = I("post.computer");
        	$data['work_year'] = I("post.work_year");
        	$data['target'] = I("post.target");
        	$data['contacts_name'] = I("post.contacts_name");
        	$data['contacts_relation'] = I("post.contacts_relation");
        	$data['contacts_phone'] = I("post.contacts_phone");

            // var_dump($data);
            // exit;

        	$info = $mod->where($where)->save($data);

        	if($info){
        		$this->success("修改成功",U("User/preInfo"));
        	}else{
        		$this->error("修改失败");
        	}

        }

        //上传头像
        public function upload(){
        	$arr = array("id"=>I("post.id"));
        	$upload = new \Think\Upload();
        	$upload->maxSize=0;
        	$upload->autoSub=false;//关闭子目录
        	$upload->exts=array("jpg","jpeg","gif",'png');
        	$upload->rootPath="./Public/photo/";
        	$info = $upload->upload();

        	if(!$info){
        		$this->error("图片上传失败");
        	}else{
        		//上传之后进行缩放
        		foreach($info as $file){
        			$img = new \Think\Image();
        			$img->open("./Public/photo/".$file['savepath'].$file['savename']);
        			$img->thumb(100,100)->save("./Public/photo/".$file['savepath']."t_".$file['savename']);
        			$mod = D("users");
                    // $users = D("users");
                    $arrs = array("id"=>$_SESSION['userid']);
                    $aa = $mod->where($arrs)->find();
                    if($aa['pic']!="noavatar.gif"){
                        unlink("./Public/photo/".$aa['pic']);
                    }
        			$data['pic'] = $file['savepath']."t_".$file['savename'];
        			$info = $mod->where($arr)->save($data);
        			
        			if($info){
        				$dir = opendir("./Public/photo/");
                        
                        unlink("./Public/photo/".$file['savepath'].$file['savename']);
        				$this->success("头像修改成功",U("User/basic"));
        			}else{
        				$this->error($mod->getError());
        			}
        		}
        	}
        }

        //查找初始密码
        public function findpass(){
            $arr = array("id"=>$_SESSION['userid']);
            $oldpassword = md5(I("post.oldpassword"));
            $users = D("users");
            $info = $users->where($arr)->find();
            if($oldpassword==$info['password']){
                echo "成功";
            }else{
                echo "失败";
            }
        }

        //修改密码
        public function updatePass(){
            $arr = array("id"=>$_SESSION['userid']);
            $users = D("users");
            $pass = array("password"=>md5(I("post.newpassword")));
            $info = $users->where($arr)->save($pass);
            if($info){
                // echo "成功";
                $this->success("密码重置成功",U("User/basic"));
            }else{
                // echo "失败";
                $this->success("密码重置失败",U("User/basic"));
            }
        }
        
    }

?>
